import * as types from "./mutation-types";
import Api from "api/api";
import _ from "lodash";
const state = {
  departments: {},
  editingDepart: {},
  currentDepart: {},
  departList: [],
  departPageStatus: {
    pageSize: 10,
    departId: "",
    type: "",
    name: ""
  },
  pageSize: 10
};

const getters = {
  departments: state => state.departments,
  editingDepart: state => state.editingDepart,
  currentDepart: state => state.currentDepart,
  departPageStatus: state => state.departPageStatus,
  departList: state => state.departList
};

const actions = {
  getDepartments({ commit }, param) {
    return Api.getDepartPage(param).then(ret => {
      commit(types.UPDATE_DEPARTMENT, ret.data.data);
      return ret;
    });
  },
  getDepartListS({ commit }, param) {
    return Api.getDepartList(param).then(ret => {
      commit(types.UPDATE_DEPARTMENT_LIST, ret.data.data);
      return ret;
    });
  },
  getOneDepartment({ commit }, id) {
    return Api.getDepart({ id: id }).then(ret => {
      console.log(ret.data.data);
      commit(types.UPDATE_CURRENT_DEPARTMENT, ret.data.data);
      return ret;
    });
  },
  addDepartment({ commit }, payload) {
    return Api.addDepart({ body: payload }).then(ret => {
      return ret;
    });
  },
  updateOneDepartments({ commit }, payload) {
    return Api.updateDepart({ body: payload }).then(ret => {
      return ret;
    });
  },

  deleteDepartments({ commit }, param) {
    console.log(param);
    return Api.deleteDepart(param).then(ret => {
      return ret;
    });
  },
  resetDepartPwd({ commit }, param) {
    return Api.resetComPassword(param).then(ret => {
      return ret;
    });
  }
};

const mutations = {
  [types.INIT_CURRENT_DEPARTMENT](state) {
    state.currentDepart = {
      name: "",
      area: "",
      address: "",
      person: "",
      phone: "",
      pwd: "",
      orderIndex: 0,
      description: ""
    };
  },
  [types.UPDATE_DEPARTMENT](state, payload) {
    state.departments = payload;
  },
  [types.UPDATE_EDITING_DEPARTMENT](state, payload) {
    state.editingDepart = payload;
  },
  [types.UPDATE_CURRENT_DEPARTMENT](state, payload) {
    state.currentDepart = payload;
  },
  [types.UPDATE_DEPARTMENT_PAGE_STATUS](state, payload) {
    state.departPageStatus = payload;
  },
  [types.UPDATE_DEPARTMENT_LIST](state, payload) {
    state.departList = payload;
  }
};
export default {
  state,
  getters,
  actions,
  mutations
};
